import java.util.*;

public class Solution {
    public int maxProfit (int[] prices) {
        if(prices.length==0) return 0;
        int firsthold=prices[0];
        int firstsell=0;
        int secondhold=prices[0];
        int secondsell=0;
        
        for(int price:prices){
            firsthold=Math.min(firsthold,price);
            firstsell=Math.max(firstsell,price-firsthold);
            secondhold=Math.min(secondhold,price-firstsell);
            secondsell=Math.max(secondsell,price-secondhold);
        }
        
        return secondsell;
    }
}